package org.jeecg.modules.orderDistributeLeafletsModule.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.orderDistributeLeafletsModule.entity.sysuserOrderEntity;
import org.jeecg.modules.orderDistributeLeafletsModule.mapper.orderDistributeLeafletMapper;
import org.jeecg.modules.orderDistributeLeafletsModule.service.orderDistributeLeafletService;
import org.jeecg.modules.zhouAdmin.admin.orderCenterModule.service.IOrderCenterService;
import org.jeecg.modules.zhoupackage.mapper.MyTrackingOrderMapper;
import org.jeecg.modules.zhoupackage.pojo.MlccTrackingOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class orderDistributeLeafletServiceImpl implements orderDistributeLeafletService {

    @Autowired
    orderDistributeLeafletMapper mapper;

    @Autowired
    IOrderCenterService service;

    @Autowired
    MyTrackingOrderMapper trackingOrderMapper;

    /**
     * 将订单派给供应商
     *
     * @param entity
     * @return
     */
    @Override
    public int distributeOrdertoSysuser(sysuserOrderEntity entity) {
        entity.setCreateTime(new Date());
        int i = mapper.distributeOrdertoSysuser(entity);
        if (entity.getOrderType() == 1 && i > 0) {
            // 订单跟踪表添加记录，已接单
            // 个订插入成本项
            service.savePaidanChengben(entity.getOrderSn(), entity.getSysuserId());
            // 个订插入工艺成本
            service.savePaidanGY(entity.getOrderSn(), entity.getSysuserId());
        }
        if (entity.getOrderType() == 2 && i > 0) {
            // 团订插入加价成本项
            service.savePaidanChengbenTean(entity.getOrderSn(), entity.getSysuserId());
            // 团订插入工艺成本
            service.savePaidanGYTeam(entity.getOrderSn(), entity.getSysuserId());
        }
        return i;
    }

    /**
     * 将订单派给供应商后 改变订单的状态
     *
     * @param orderId
     * @param orderType
     */
    @Override
    public void updateorderstatus(sysuserOrderEntity entity, String orderId, int orderType) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (orderType == 0) {
            mapper.updatemianliaostatus(Integer.parseInt(orderId));
            // 0未支付，1已支付，2申请售后，3已接单，4已发货，5已完成，6备库 7驳回
            addtrackingOrder(entity.getOrderSn(), 0, "订单已接单", user.getRealname(), 3, "后台已接单");
        } else if (orderType == 1) {
            mapper.updategedingstatus(Integer.parseInt(orderId));
            // 0未支付，1已支付，2申请售后，3已接单，4已发货，5已完成，6备库 7驳回
            addtrackingOrder(entity.getOrderSn(), 1, "订单已接单", user.getRealname(), 3, "后台已接单");
        } else if (orderType == 2) {
            mapper.updateteamstatus(Integer.parseInt(orderId));
            // 0未支付，1已支付，2申请售后，3已接单，4已发货，5已完成，6备库 7驳回
            addtrackingOrder(entity.getOrderSn(), 2, "订单已接单", user.getRealname(), 3, "后台已接单");
        }
    }

    /**
     * 往订单跟踪表添加数据
     *
     * @param orderSn     订单编号
     * @param orderType   订单类型
     * @param remark      备注
     * @param username    更新人
     * @param orderStatus 0未支付，1已支付，2申请售后，3已接单，4已发货，5已完成，6备库
     * @param thing       订单事件
     */
    public void addtrackingOrder(String orderSn, int orderType, String remark, String username, Integer orderStatus, String thing) {
        MlccTrackingOrder trackingOrder = new MlccTrackingOrder();
        trackingOrder.setOrderSn(orderSn).setOrderType(orderType).setRemark(remark).setThing(thing)
                .setCreateBy(username).setUpdateBy(username).setOrderStatus(orderStatus);
        trackingOrderMapper.insert(trackingOrder);
    }
}
